package de.fmannan.wildit.ui;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import de.fmannan.wildit.persistence.PersistenceAdapter;
import de.fmannan.wildit.persistence.PersistenceProvider;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

public class RealityCheckSettings extends Activity {

	private static final String TAG = "RealityCheckSettings";

	private PersistenceAdapter persistenceAdapter;

	TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		}
	};

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		persistenceAdapter = PersistenceProvider.getPersistenceProvider(this);
		// Load the layout
		setContentView(R.layout.realitychecksettingslayout);

		updateLabels();

		addListeners();

	}

	private void updateLabels() {
		Calendar time;
		// Update end time field with set time
		TextView endTime = (TextView) findViewById(R.id.rcEndTime_label);
		time = persistenceAdapter.getAlarmEndTime();
		endTime.setText(time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE));
		endTime.setText(formatTime(time));

		// Update Start time field with set time
		TextView startTime = (TextView) findViewById(R.id.rcStartTime_label);
		time = persistenceAdapter.getAlarmStartTime();
		startTime.setText(time.get(Calendar.HOUR_OF_DAY) + ":" + time.get(Calendar.MINUTE));
		startTime.setText(formatTime(time));

		// Update reminder interval field with set time
		TextView intervalTime = (TextView) findViewById(R.id.rcReminderInterval_label);
		time = persistenceAdapter.getAlarmInterval();
		intervalTime.setText(formatTime(time));
	}
	
	/**
	 * Adds padding in form of a zero to hours or minutes - depending on whether it
	 * is required. The output format returned as as a string is: HH:mm.
	 */
	private String formatTime(Calendar time)
	{
		int hours = time.get(Calendar.HOUR_OF_DAY);
		int minutes = time.get(Calendar.MINUTE);
		
		String result = hours < 10 ? "0" + String.valueOf(hours) : String.valueOf(hours);
		result += ":";
		result += minutes < 10 ? "0" + String.valueOf(minutes) : String.valueOf(minutes);
		
		return result;
	}

	private void addListeners() {
		TextView startTime = (TextView) findViewById(R.id.rcStartTime);
		startTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					v.getContext().startActivity(
							new Intent(v.getContext(),
									RealityCheckStartTimePicker.class));
				} catch (ActivityNotFoundException exc) {
					Log.e(TAG,"The activity "
											+ RealityCheckStartTimePicker.class
											+ " has not been found. Make sure it's in your AndroidManifest.xml");
				}
			}
		});
		TextView endTime = (TextView) findViewById(R.id.rcEndTime);
		endTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					v.getContext().startActivity(
							new Intent(v.getContext(),
									RealityCheckEndTimePicker.class));
				} catch (ActivityNotFoundException exc) {
					Log.e(TAG,"The activity "
											+ RealityCheckEndTimePicker.class
											+ " has not been found. Make sure it's in your AndroidManifest.xml");
				}
			}
		});
		TextView intervalTime = (TextView) findViewById(R.id.rcReminderInterval);
		intervalTime.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					v.getContext().startActivity(
							new Intent(v.getContext(),
									RealityCheckReminderIntervalPicker.class));
				} catch (ActivityNotFoundException exc) {
					Log.e(TAG,"The activity "
											+ RealityCheckReminderIntervalPicker.class
											+ " has not been found. Make sure it's in your AndroidManifest.xml");
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		updateLabels();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}